package Dashboard;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.swing.JLabel;
import javax.swing.JPanel;

import UserInput.DashboardInput;

import Gauges.GaugeName;

/**
 * 
 * @author Sietse
 *
 */
public class DashboardView extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 397861591030975756L;
	//The background images
	private BufferedImage oceanBackground;
	private BufferedImage dashboardBackground;
	
	//The sub-panels
	private MainViewPanel mainViewPanel;  
	private ImagePanel dashboardBackgroundPanel;
	private ImagePanel oceanBackgroundPanel;
	
	private Dimension screenSize;
	
	//The labels (for clicking)
	private JLabel controlAutomaticClick;
	private JLabel controlManualClick;
	private JLabel keelUpClick;
	private JLabel keelDownClick;
	private JLabel motorForwardClick;
	private JLabel motorBackwardClick;
	private JLabel motorNeutralClick;
	private JLabel emergencySituationClick;
	private JLabel emergencyStopClick;
	
	//The single image which will be painted on this panel
	private Image offscreen;
	
	//The Graphics corresponding with the above image
	private Graphics offscreenGraphics;

	/**
	 * Initializes the panels and the labels
	 */
	public DashboardView() 
	{	
        this.setDoubleBuffered(true);
        this.setOpaque(true);
        
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        
        this.setDoubleBuffered(true);
        this.setOpaque(true);

        initPanels(); 
		initButtonLabels();
        
 
	}
	
	/**
	 * Initializes the invisible labels behind each dashboard button.
	 * These labels are clickable so it is as if one clicks the buttons.
	 */
	private void initButtonLabels()
	{
		//Initialize
		controlAutomaticClick = new JLabel(); 
    	controlManualClick = new JLabel();
    	keelUpClick = new JLabel();
    	keelDownClick = new JLabel();
    	motorForwardClick = new JLabel();
    	motorBackwardClick = new JLabel();
    	motorNeutralClick = new JLabel();
    	emergencySituationClick = new JLabel();
    	emergencyStopClick = new JLabel();  
    	
    	//Give them names
    	controlAutomaticClick.setName(String.valueOf(ButtonNames.CONTROLAUTOMATIC));
    	controlManualClick.setName(String.valueOf(ButtonNames.CONTROLMANUAL));
    	keelUpClick.setName(String.valueOf(ButtonNames.KEELUP));
    	keelDownClick.setName(String.valueOf(ButtonNames.KEELDOWN));
    	motorForwardClick.setName(String.valueOf(ButtonNames.MOTORFORWARD));
    	motorBackwardClick.setName(String.valueOf(ButtonNames.MOTORBACKWARD));
    	motorNeutralClick.setName(String.valueOf(ButtonNames.MOTORNEUTRAL));
    	emergencySituationClick.setName(String.valueOf(ButtonNames.EMERGENCYSITUATIONOFF));
    	emergencyStopClick.setName(String.valueOf(ButtonNames.EMERGENCYSTOP));
    	
    	//Add them to this panel
	    this.add(controlAutomaticClick);
	    this.add(controlManualClick);
	    this.add(keelUpClick);
	    this.add(keelDownClick);
	    this.add(motorBackwardClick);
	    this.add(motorForwardClick);
	    this.add(motorNeutralClick);
	    this.add(emergencySituationClick);
	    this.add(emergencyStopClick);    
	}
	
	/**
	 * Initialize all sub-panels
	 */
	private void initPanels()
	{
		//Initialize the MainViewPanel
        mainViewPanel = new MainViewPanel();     
        
        //Load the background images
        oceanBackground = mainViewPanel.loadImage("/Resources/Images/OceanBackgroundLarge.jpg");
        dashboardBackground = mainViewPanel.loadImage("/Resources/Images/dashboardBackground.gif");
        
        //Initialize the backgroundpanels
        oceanBackgroundPanel = new ImagePanel(oceanBackground);
        dashboardBackgroundPanel = new ImagePanel(dashboardBackground);	     
	}

	/**
	 * Initializes an image, which can be filled off screen
	 */
	private void obtainOffScreen() 
	{
		if (this.offscreen == null) 
		{
			this.offscreen = this.createImage(screenSize.width, screenSize.height - 21); //-21 for the space taken up by the frame's menu
			this.offscreenGraphics = this.offscreen.getGraphics();
		}
	}
	public void paintGaugesNow()
	{
		int mainPanelOffsetX = ((this.getWidth() - 1000 )/ 2); 
		int mainPanelOffsetY = (this.getHeight() - 650);
		
		this.paintImmediately(mainPanelOffsetX, mainPanelOffsetY, mainPanelOffsetX + 1000, mainPanelOffsetY + 650);
	}
	
	
	@Override
	public void paintComponent(Graphics g)
	{	
		int height = this.getHeight();
		int width = this.getWidth();

		
		int mainPanelOffsetX = ((width - 1000 )/ 2); 
		int mainPanelOffsetY = (height - 650);
		obtainOffScreen();
		
		//Add the three sub-panels to the offscreen
		oceanBackgroundPanel.paintM(g);
		dashboardBackgroundPanel.paintM(g, 0, 0);	
		mainViewPanel.paintM(g, mainPanelOffsetX, mainPanelOffsetY);
	
		
		//Draw the offscreen
		//g.drawImage(offscreen, 0, 0, this.getWidth(), height - 20, this);
			

		//Button labels for clicking
		int x,y;
		
		//Control
		x = mainPanelOffsetX + 150;
		y = mainPanelOffsetY + 400;
	    controlAutomaticClick.setBounds(x, y, 50, 50);
	    //repaint(x,y,50,50);
	    
	    y = mainPanelOffsetY + 460;
	    controlManualClick.setBounds(x, y, 50, 50);
	    //repaint(x,y,50,50);
	    
	    //Keel
	    x = mainPanelOffsetX + 325;
	    y = mainPanelOffsetY + 400;
	    keelUpClick.setBounds(x, y, 50, 50);
	    //repaint(x,y,50,50);
	    
	    y = mainPanelOffsetY + 460;
	    keelDownClick.setBounds(x, y, 50, 50);
	    //repaint(x,y,50,50);
	    
	    //Motor
	    x = mainPanelOffsetX + 500;
	    y = mainPanelOffsetY + 400;
	    motorForwardClick.setBounds(x, y, 50, 50);
	   // repaint(x,y,50,50);
	    
	    y = mainPanelOffsetY + 460;
	    motorNeutralClick.setBounds(x, y, 50, 50);
	    //repaint(x,y,50,50);
	    
	    y = mainPanelOffsetY + 520;
	    motorBackwardClick.setBounds(x, y, 50, 50);
	    //repaint(x,y,50,50);
	    
	    //Emergencies
	    x = mainPanelOffsetX + 675;
	    y = mainPanelOffsetY + 400;
	    emergencySituationClick.setBounds(x, y, 50, 50);
	    //repaint(x,y,50,50);
	    
	    y = mainPanelOffsetY + 460;
	    emergencyStopClick.setBounds(x, y, 50, 50);
	    //repaint(x,y,50,50);
	    
		//int mainPanelOffsetX = ((this.getWidth() - 1000 )/ 2); 
		//int mainPanelOffsetY = (this.getHeight() - 650);
	    //repaint(mainPanelOffsetX + 150, mainPanelOffsetY + 70, mainPanelOffsetX + 850, mainPanelOffsetY + 300);
	    
	}
	
	
	/**
	 * Sets the replaylight
	 * @param value the value the replaylight needs to be set to (off/on)
	 */
	public void setReplayLight(boolean value)
	{
		if (value)
		{
			mainViewPanel.setReplayFunction(1);
		}
		else
		{
			mainViewPanel.setReplayFunction(0);
		}
	}
	
	
	
	/**
	 * Updates the images of the gauges in the dashboard
	 * @param gaugeName the name of the gauge in question
	 * @param value the value of the gauge in question
	 */
	public void updateGaugeImages(GaugeName gaugeName, int value)
	{
		switch(gaugeName)
		{
			case ANGLE: mainViewPanel.setTiltNeedleRotation(value);
				break;
			case WIND: mainViewPanel.setWindNeedleRotation(value);
				break;
			case SAIL: mainViewPanel.setSailNeedleRotation(value);
				break;
			case KEEL: mainViewPanel.setKeelButton(value);
				break;
			case MOTOR: mainViewPanel.setMotorButton(value);
				break;
			case CONTROL: mainViewPanel.setControlButton(value);
				break;
			case HELM: mainViewPanel.setRudderNeedleRotation(value);
				break;
			case EMERGENCYSITUATION: mainViewPanel.setEmergencySituationButton(value);
				break;
			case EMERGENCYSTOP: mainViewPanel.setEmergencyStopButton(value);
				break;
			case BLUETOOTH: mainViewPanel.setBluetoothConnection(value);
				break;
		}
	}
	
	/**
	 * Link the MouseListener to the widgets requiring this type of listener
	 * @param mouseListener the MouseListener
	 */
	public void linkMouseListener(DashboardInput mouseListener)
	{
		controlAutomaticClick.addMouseListener(mouseListener); 
    	controlManualClick.addMouseListener(mouseListener);
    	keelUpClick.addMouseListener(mouseListener);
    	keelDownClick.addMouseListener(mouseListener);
    	motorForwardClick.addMouseListener(mouseListener);
    	motorBackwardClick.addMouseListener(mouseListener);
    	motorNeutralClick.addMouseListener(mouseListener);
    	emergencySituationClick.addMouseListener(mouseListener);
    	emergencyStopClick.addMouseListener(mouseListener);	
	}

	/**
	 * Push bluetooth connection value to mainviewpanel
	 * @param bluetoothConnection
	 */
	public void setBluetoothConnection(int bluetoothConnection) {
		mainViewPanel.setBluetoothConnection(bluetoothConnection);
		
	}	
}
